<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Goods\Controller;

use Admin\Controller\AdminActionController;
use Admin\Data\Common;
use Doctrine\ORM\EntityManager;
use Goods\Entity\Goods;
use Goods\Entity\GoodsOtherTag;
use Goods\Entity\GoodsOtherTagGoods;
use Goods\Form\GoodsOtherTagForm;
use Goods\Service\GoodsOtherTagGoodsManager;
use Goods\Service\GoodsOtherTagManager;
use Laminas\Mvc\I18n\Translator;
use Laminas\View\Model\JsonModel;

class GoodsOtherTagController extends AdminActionController
{
    private $translator;
    private $entityManager;

    private $goodsOtherTagManager;
    private $goodsOtherTagGoodsManager;

    public function __construct(
        Translator $translator,
        EntityManager $entityManager,
        GoodsOtherTagManager $goodsOtherTagManager,
        GoodsOtherTagGoodsManager $goodsOtherTagGoodsManager
    )
    {
        $this->translator   = $translator;
        $this->entityManager= $entityManager;
        $this->goodsOtherTagManager = $goodsOtherTagManager;
        $this->goodsOtherTagGoodsManager = $goodsOtherTagGoodsManager;
    }

    public function indexAction()
    {
        $otherTagList = $this->entityManager->getRepository(GoodsOtherTag::class)->findOtherTagList();

        return ['otherTagList' => $otherTagList];
    }

    /**
     * 添加聚合标签
     * @return GoodsOtherTagForm[]|\Laminas\Http\Response
     * @throws \Doctrine\ORM\Exception\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function addAction()
    {
        $form = new GoodsOtherTagForm();

        if($this->getRequest()->isPost()) {
            $data = $this->params()->fromPost();
            $form->setData($data);
            if ($form->isValid()) {
                $data = $form->getData();
                $this->goodsOtherTagManager->addOtherTag($data);

                $this->adminCommon()->addOperLog(sprintf($this->translator->translate('聚合标签 %s 添加成功!'), $data['otherTagName']), $this->translator->translate('聚合标签'));

                return $this->redirect()->toRoute('goods-other-tag');
            }
        }

        return ['form' => $form];
    }

    /**
     * 编辑聚合标签
     * @return array|\Laminas\Http\Response
     * @throws \Doctrine\ORM\Exception\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function editAction()
    {
        $otherTagId = (int) $this->params()->fromRoute('id', -1);

        $otherTagInfo = $this->entityManager->getRepository(GoodsOtherTag::class)->findOneBy(['otherTagId' => $otherTagId]);
        if ($otherTagInfo == null) {
            $this->flashMessenger()->addWarningMessage($this->translator->translate('该聚合标签不存在!'));
            return $this->redirect()->toRoute('goods-other-tag');
        }

        $form = new GoodsOtherTagForm();

        if($this->getRequest()->isPost()) {
            $data = $this->params()->fromPost();
            $form->setData($data);
            if ($form->isValid()) {
                $data = $form->getData();
                $this->goodsOtherTagManager->editOtherTag($data, $otherTagInfo);

                $this->adminCommon()->addOperLog(sprintf($this->translator->translate('聚合标签 %s 编辑成功!'), $data['otherTagName']), $this->translator->translate('聚合标签'));

                return $this->redirect()->toRoute('goods-other-tag');
            }
        } else $form->setData($otherTagInfo->valuesArray());

        return ['form' => $form, 'otherTagInfo' => $otherTagInfo];
    }

    /**
     * 聚合标签删除
     * @return mixed
     * @throws \Doctrine\ORM\Exception\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function deleteAction()
    {
        $otherTagId = (int) $this->params()->fromRoute('id', -1);

        $otherTagInfo = $this->entityManager->getRepository(GoodsOtherTag::class)->findOneBy(['otherTagId' => $otherTagId]);
        if ($otherTagInfo == null) {
            $this->flashMessenger()->addWarningMessage($this->translator->translate('该聚合标签不存在!'));
            return $this->adminCommon()->toReferer();
        }

        $this->goodsOtherTagManager->deleteOtherTag($otherTagInfo);
        $this->goodsOtherTagGoodsManager->deleteMoreOtherTagGoods($otherTagId);

        $this->adminCommon()->addOperLog(sprintf($this->translator->translate('聚合标签 %s 删除成功!'), $otherTagInfo->getOtherTagName()), $this->translator->translate('聚合标签'));

        return $this->adminCommon()->toReferer();
    }

    /**
     * 设置聚合标签商品
     * @return array
     */
    public function editTagGoodsAction()
    {
        $otherTagId = (int) $this->params()->fromRoute('id', -1);

        $otherTagInfo = $this->entityManager->getRepository(GoodsOtherTag::class)->findOneBy(['otherTagId' => $otherTagId]);
        if ($otherTagInfo == null) {
            $this->flashMessenger()->addWarningMessage($this->translator->translate('该聚合标签不存在!'));
            return $this->adminCommon()->toReferer();
        }

        $otherTagGoodsList = $this->entityManager->getRepository(GoodsOtherTagGoods::class)->findOtherTagGoodsList($otherTagId);

        return ['otherTagInfo' => $otherTagInfo, 'otherTagGoodsList' => $otherTagGoodsList];
    }

    /**
     * 添加聚合标签商品
     * @return JsonModel
     * @throws \Doctrine\ORM\Exception\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function addOtherTagGoodsAction()
    {
        $goodsId    = (int) $this->params()->fromPost('goodsId', -1);
        $otherTagId = (int) $this->params()->fromPost('otherTagId', -1);

        $goodsInfo  = $this->entityManager->getRepository(Goods::class)->findOneBy(['goodsId' => $goodsId]);
        if ($goodsInfo) {
            $otherTagGoods = $this->entityManager->getRepository(GoodsOtherTagGoods::class)->findOneBy(['goodsId' => $goodsId, 'otherTagId' => $otherTagId]);
            if ($otherTagGoods == null) {
                $addState = $this->goodsOtherTagGoodsManager->addOtherTagGoods([
                    'otherTagId'=> $otherTagId,
                    'goodsId'   => $goodsId
                ]);
                return new JsonModel([
                    'state' => 'ok',
                    'result'=> [
                        'otherTagGoodsId'   => $addState->getOtherTagGoodsId(),
                        'goodsName'         => $goodsInfo->getGoodsName(),
                        'goodsState'        => Common::goodsState($this->translator)[$goodsInfo->getGoodsState()],
                        'goodsPrice'        => Common::configValue('default', 'currency')['symbol'] . $goodsInfo->getGoodsPrice(),
                        'otherTagGoodsSort' => 255
                    ]
                ]);
            }
        }
        return new JsonModel(['state' => 'error']);
    }

    /**
     * 批量排序处理
     * @return mixed
     * @throws \Doctrine\ORM\Exception\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     * @throws \Doctrine\Persistence\Mapping\MappingException
     */
    public function updateOtherTagGoodsSortAction()
    {
        if($this->getRequest()->isPost()) {
            $data = $this->params()->fromPost();
            $this->goodsOtherTagGoodsManager->updateOtherTagGoodsSort($data);

            $this->adminCommon()->addOperLog($this->translator->translate('排序处理成功!'), $this->translator->translate('聚合标签'));
        }
        return $this->adminCommon()->toReferer();
    }

    /**
     * 删除聚合标签的单个商品
     * @return JsonModel
     * @throws \Doctrine\ORM\Exception\ORMException
     * @throws \Doctrine\ORM\OptimisticLockException
     */
    public function deleteOtherTagGoodsAction()
    {
        $otherTagGoodsId    = (int) $this->params()->fromRoute('id', -1);
        $otherTagGoodsInfo  = $this->entityManager->getRepository(GoodsOtherTagGoods::class)->findOneBy(['otherTagGoodsId' => $otherTagGoodsId]);
        if ($otherTagGoodsInfo == null) return new JsonModel(['msg' => $this->translator->translate('该聚合标签商品不存在')]);

        $this->goodsOtherTagGoodsManager->deleteOtherTagGoods($otherTagGoodsInfo);

        return new JsonModel(['state' => 'ok']);
    }
}